package com.nbst.controller.base;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nbst.comnutil.DESUtil;
import com.nbst.comnutil.DateUtil;
import com.nbst.comnutil.NormalResult;
import com.nbst.model.base.DataBase;
import com.nbst.model.base.User;
import com.nbst.service.base.IDataBaseService;
import com.nbst.service.base.IUserService;

/**
 * @ClassName: DataBaseController
 * @Description:基础设置
 * @author 兵
 * @date 2018-6-4
 * 
 */
@RestController
@RequestMapping("/dataBase")
public class DataBaseController {

	@Autowired
	IDataBaseService dataBaseService;

	@Autowired
	IUserService userservice;

	/**
	 * 获取关帐设置信息
	 * 
	 * @return NormalResult
	 * @author 兵
	 * @date 2018-6-4
	 */
	@PostMapping("/getAccount.action")
	public Object getAccount() {
		List<DataBase> list = dataBaseService.getCloseAccount();
		if (list.size() != 1) {
			return null;
		}
		Map<String,Object> map = new HashMap<>(12);
		map.put("code", "关账时间设置");
		map.put("startTime", list.get(0).getCjsj());
		map.put("endTime", list.get(0).getSxsj());
		if ((list.get(0).getCjsj().getTime()) < (System.currentTimeMillis())) {
			if (System.currentTimeMillis() < list.get(0).getSxsj().getTime()) {
				map.put("status", "关账中");
			}
		}else {
			map.put("status", "未关账");
		}
		return map;

	}

	/**
	 * 撤销关帐设置
	 * 
	 * @param password
	 *            管理员密码
	 * @return NormalResult
	 * @author 兵
	 * @date 2018-6-4
	 */
	@PostMapping("/openAccount.action")
	public NormalResult openAccount(String password) {
		Map<String,Object> map = new HashMap<>(6);
		map.put("uAccount", "admin");
		map.put("uPassword", DESUtil.encrypt(password));
		map.put("sfyx", "1");
		List<User> list = userservice.findByCondition(map);
		if (list.size() == 1) {
			DataBase base = dataBaseService.getCloseAccount().get(0);
			if (base != null) {
				return dataBaseService.updateCloseAccount(base, 1);
			} else {
				return new NormalResult("9999", "撤销失败");
			}
		} else {
			return new NormalResult("9999", "密码有误");
		}

	}

	/**
	 * 关帐设置
	 * 
	 * @param endTime
	 * @return NormalResult
	 * @author 兵
	 * @throws ParseException 
	 * @date 2018-6-4
	 */
	@PostMapping(value = "/closeAccount.action")
	public NormalResult getCodeList(Date endTime, String password,HttpSession session) throws ParseException {
		if(StringUtils.isEmpty(password)){
			return new NormalResult("9999", "请输入管理员(admin)密码");
		}
		Map<String,Object> map = new HashMap<>(6);
		map.put("uAccount", "admin");
		map.put("uPassword", DESUtil.encrypt(password));
		map.put("sfyx", "1");
		List<User> list = userservice.findByCondition(map);
		endTime = DateUtil.String2Date("2118-01-01");
		if (list.size() == 1) {
			if (endTime == null) {
				return new NormalResult("9999", "请输入关帐结束日期");
			}
			DataBase base = dataBaseService.getCloseAccount().get(0);
			if (base != null) {
				base.setSxsj(endTime);
			}
			return dataBaseService.updateCloseAccount(base, 0);
		} else {
			return new NormalResult("9999", "密码有误");
		}
	}
}
